﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WStest.Models;

namespace WStest.Controllers
{
    public class MessageController : Controller
    {
        Service service = new Service();
        //
        // GET: /Message/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Received()
        {
            UserView u = service.GetUserByUsername(User.Identity.Name);
            List<PrivatemessageView> received = service.GetPrivateMessagesReceivedByUserId(u.Id);
            return View(received);
        }

        public ActionResult Sent()
        {
            UserView u = service.GetUserByUsername(User.Identity.Name);
            List<PrivatemessageView> sent = service.GetPrivateMessagesSentByUserId(u.Id);
            return View(sent);
        }
        //
        // GET: /Message/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Message/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Message/Create

        [HttpPost]
        public ActionResult Create(PrivatemessageView pmv)
        {
            try
            {
                UserView u = service.GetUserByUsername(User.Identity.Name);
                if (service.SubmitPrivateMessage(u.Id, pmv.ToUser, pmv.Title, pmv.Content))
                {
                    return RedirectToAction("Index");
                }
                return View();                
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Message/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Message/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Message/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Message/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
